define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            let vue = new Vue({
                el: '#vue',
                data: {
                    uploadLoading: '',
                    uploadData: {},
                    navList: [],
                    // 表格当前页数据
                    tableData: [],
                    // 多选数组
                    multipleSelection: [],
                    // 搜索条件
                    key: '',
                    // 默认每页数据量
                    pageSize: 10,
                    // 当前页码
                    currentPage: 1,
                    // 默认数据总数
                    totalCount: 0,
                    // 列表加载遮罩
                    listLoading: false,
                    // 搜索字段
                    search: {
                        startTime: '',
                        endTime: '',
                        keyword: '',
                        type: '',
                        sort: 'id',
                        typeIndex: '',
                        category: '',
                        nav: '',
                        order: 'id DESC'
                    },
                    // 类型
                    typeList: [{
                        index: 1,
                        name: '图片',
                        key: 'image/'
                    }, {
                        index: 2,
                        name: '音频',
                        key: 'audio/'
                    }, {
                        index: 3,
                        name: '视频',
                        key: 'video/'
                    }, {
                        index: 4,
                        name: '文档',
                        key: 'text/'
                    }, {
                        index: 5,
                        name: '应用',
                        key: 'application/'
                    }, {
                        index: 6,
                        name: '压缩包',
                        key: 'zip/'
                    }],
                    searchOpt: [{
                        id: 1,
                        value: '文件名'
                    }],
                    sendStartTime: '',
                    sendEndTime: '',
                    navDialog: false,
                    formData: {
                        nav: ''
                    },
                    // 时间格式化
                    dateFormat: function (row, column) {
                        var date = row[column.property];
                        if (date === undefined || date === 0) {
                            return '';
                        }
                        return moment(date * 1000).format("YYYY-MM-DD HH:mm:ss");
                    }
                },
                created() {
                    this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                },
                methods: {
                    // 打开归类
                    handleNavDialog() {
                        if (this.multipleSelection.length === 0) {
                            this.$message.warning('请选择至少一个附件进行归类！');
                            return;
                        }
                        this.formData = {
                            nav: ''
                        };
                        this.navDialog = true;
                    },
                    // 提交
                    submitNav() {
                        if (this.formData.nav === '') {
                            this.$message.warning('请您选择分类！');
                            return;
                        }
                        // 提交
                        let loading = this.$loading({
                            lock: true,
                            text: '正在归类中...',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.3)'
                        });

                        var array = [];
                        this.multipleSelection.forEach((row) => {
                            array.push(row.id);
                        });
                        this.formData.ids = array;

                        $.ajax({
                            url: 'general/file/nav',
                            type: 'POST',
                            data: this.formData,
                            success: res => {
                                loading.close();
                                if (res.code === 200) {
                                    this.$message.success(res.msg);
                                    this.navDialog = false;
                                    this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                                } else {
                                    this.$message.warning(res.msg);
                                }
                            }
                        })
                    },
                    // 上传前
                    beforeFileUpload(file) {
                        // 文件大小验证
                        if (file.size / 1024 / 1024 > 10) {
                            this.$message.warning('上传的图片大小不能超过10M');
                            return false;
                        }

                        this.uploadLoading = this.$loading({
                            lock: true,
                            text: '正在上传中...',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.1)'
                        });
                    },
                    // 上传成功后
                    uploadFileSuccess(res, file, fileList) {
                        if (res.code === 1) {
                            this.uploadLoading.close();
                            this.$message.success(res.msg);
                            this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                        } else {
                            this.$message.warning(res.msg);
                        }
                    },
                    handleClearSelect(field) {
                        // 重新加载
                        this.$set(this.search, field, '');
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                    },
                    handleClearSearchInp() {
                        if (this.search.type) {
                            // 重新加载
                            this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                        }
                    },
                    // 刷新页面
                    refreshPage() {
                        this.search = {
                            startTime: '',
                            endTime: '',
                            keyword: '',
                            type: '',
                            sort: 'id',
                            category: '',
                            typeIndex: '',
                            nav: '',
                            order: 'id DESC'
                        };
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                    },
                    // 打开分类管理
                    handleOpenNav() {
                        Fast.api.open("general/nav/index", '分类管理', {
                            area: ['1200px', '720px'],
                            callback: res => {

                            }
                        });
                    },
                    // 选择图片
                    selectImage() {
                        Fast.api.open("general/attachment/select?multiple=false", '选择图片', {
                            callback: res => {
                                this.$set(this.formData, 'image', res.url);
                            }
                        });
                    },
                    // 从服务器读取数据
                    loadData(key, currentPage, pageSize, order) {
                        this.listLoading = true;
                        $.getJSON('general/file/index', {
                            keyword: key,
                            type: this.search.type,
                            status: this.search.status,
                            nav: this.search.nav,
                            start_time: this.sendStartTime,
                            end_time: this.sendEndTime,
                            category: this.search.category,
                            mimetype: this.search.typeIndex > 0 ? this.typeList[this.search.typeIndex - 1].key :'',
                            page: currentPage,
                            rows: pageSize,
                            order,
                        }, data => {
                            this.totalCount = data.total;
                            this.tableData = data.list;
                            this.navList = data.navList;
                            this.listLoading = false;
                        });
                    },
                    handleNavChange(e) {
                        if (e.length === 0) {
                            // 重新加载
                            this.$set(this.search, 'nav', '');
                            this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                        }
                    },
                    handleSelectCategory(index) {
                        for (let item of this.categoryOpt) {
                            item.selected = false;
                        }
                        this.categoryOpt[index].selected = true;
                        this.search.category = this.categoryOpt[index].category === 'all' ? '' : this.categoryOpt[index].category;

                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                    },
                    // 表格字段排序
                    handleTableFieldSort(e) {
                        let orderBy = e.order === 'ascending' ? 'ASC' : 'DESC';
                        this.search.order = `${e.prop} ${orderBy}`;
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                    },
                    // 搜索
                    searchData() {
                        this.sendStartTime = this.search.startTime !== '' ? +this.search.startTime / 1000 : '';
                        this.sendEndTime = this.search.endTime !== '' ? +this.search.endTime / 1000 : '';
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                    },
                    // 每页显示数据量变更
                    handleSizeChange(val) {
                        this.pageSize = val;
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                    },
                    // 页码变更
                    handleCurrentChange(val) {
                        this.currentPage = val;
                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                    },
                    // 多选响应
                    handleSelectionChange(val) {
                        this.multipleSelection = val;
                    },
                    // 批量删除
                    del() {
                        if (this.multipleSelection.length === 0) {
                            this.$message.warning('请选择至少一个附件进行删除！');
                            return;
                        }
                        var array = [];
                        this.multipleSelection.forEach((row) => {
                            array.push(row.id);
                        });

                        this.$confirm('您确定要删除所选的附件吗？', '温馨提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            let loading = this.$loading({
                                lock: true,
                                text: '正在删除中..',
                                spinner: 'el-icon-loading',
                                background: 'rgba(0, 0, 0, 0.3)'
                            });
                            $.ajax({
                                url: 'general/attachment/del',
                                data: "ids=" + array,
                                type: "POST",
                                dataType: 'json',
                                success: res => {
                                    loading.close();
                                    if (res.code === 1) {
                                        this.$message.success('删除附件成功');
                                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                                    } else {
                                        this.$message.warning(res.msg);
                                    }
                                },
                                error: function (er) {

                                }
                            });
                        }).catch(() => {

                        });
                    },
                    // 单条删除
                    dell(id) {
                        this.$confirm('您确定要删除此附件吗？', '温馨提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            let loading = this.$loading({
                                lock: true,
                                text: '正在删除中..',
                                spinner: 'el-icon-loading',
                                background: 'rgba(0, 0, 0, 0.3)'
                            });
                            $.ajax({
                                url: 'general/attachment/del',
                                data: {
                                    ids: id
                                },
                                type: "POST",
                                dataType: 'json',
                                success: res => {
                                    loading.close();
                                    if (res.code === 1) {
                                        this.$message.success('删除附件成功');
                                        this.loadData(this.search.keyword, this.currentPage, this.pageSize, this.search.order);
                                    } else {
                                        this.$message.warning(res.msg);
                                    }
                                },
                                error: function (er) {

                                }
                            });
                        }).catch(() => {

                        });
                    },
                }
            })
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
